package com.gr.service.impl;

import com.gr.dao.TeaMapper;
import com.gr.entity.Tea;
import com.gr.service.TeaService;
import com.gr.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class TeaServiceImpl implements TeaService {
    @Override
    public List<Tea> selectAll() {
        List<Tea> teas=null;
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper tm = session.getMapper(TeaMapper.class);
            teas=tm.selectAll();

        } finally {
            MybatisUtils.closeSession(session);
        }
        return teas;
    }

    @Override
    public void insertTea(Tea tea) {
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper tm = session.getMapper(TeaMapper.class);
            tm.insertTea(tea);
            session.commit();
        } finally {
            MybatisUtils.closeSession(session);
        }
    }

    @Override
    public void deleteTeaById(Integer id) {
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper tm = session.getMapper(TeaMapper.class);
            tm.deleteTeaById(id);
            session.commit();
        } finally {
            MybatisUtils.closeSession(session);
        }
    }


    @Override
    public void updateTea(Tea tea) {
        /**
         *
         */
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper tm = session.getMapper(TeaMapper.class);
            tm.updateTea(tea);
            session.commit();
        } finally {
            MybatisUtils.closeSession(session);
        }
    }

    @Override
    public Tea selectOne(Integer id) {
        Tea tea=null;
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper tm = session.getMapper(TeaMapper.class);
            tea=tm.selectOne(id);

        } finally {
            MybatisUtils.closeSession(session);
        }
        return tea;
    }

    @Override
    public void deleteTeaByBid(Integer bid) {
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper tm = session.getMapper(TeaMapper.class);
            tm.deleteTeaByBid(bid);
            session.commit();
        } finally {
            MybatisUtils.closeSession(session);
        }
    }

    @Override
    public List<Tea> selectTeaByTypeID(Integer tid) {
        List<Tea> teas=null;
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper tm = session.getMapper(TeaMapper.class);
            teas=tm.selectTeaByTypeID(tid);

        } finally {
            MybatisUtils.closeSession(session);
        }
        return teas;
    }

}
